
local ClientOHOH = _G.ClientOHOH;
local M = ClientOHOH.class.new(script, ClientOHOH.files.Client_PlayerComponent(),script.Parent.Name);
local EventList = ClientOHOH.load.EventList();
local RunService = game:GetService("RunService");
local ConnectClass=_G.CommonOHOH.load.Connect();
local FunctionList = ClientOHOH.load.FunctionList();
local UIManager = ClientOHOH.load.UIManager();
local PVPGui=UIManager.ui_list.PVPGui
function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent);
    self.player=parent
	self.hadBoom =false
	self.PVPOverEvent=ConnectClass.new() --碰撞事件
	
end;

function M.dtor(self)
	M.super.dtor(self);
end;
function M.ClearPVPTrack(self)

    if self.Car then
        self.Car:dtor()
        self.Car=nil
    end
    if  self.EnemyCar then
        self.EnemyCar:dtor()
        self.EnemyCar=nil
    end

end

--测试 单个车辆性能时候使用
function M.CreatePVPTestTrack(self,isWin)
	print("测试 非PVP")
	self.hadOver = false

	local TrackAttRoot=game.Workspace:WaitForChild("PVPTrack")
	local st1 = TrackAttRoot:WaitForChild("Start01")
	local st2 = TrackAttRoot:WaitForChild("Start02")

    self.HeartbeatUpdateConnect = nil
    self:ClearPVPTrack()
	self.Car=_G.CommonOHOH.load.PVPCar().new(self.parent,"10002_1","50002_1","20001_1","30002_1","40001_1")

	self.Car:SetCFrame(st1.CFrame)
    self.Car:SetParent(workspace)
	self.Car:Start()
	self.RootPart = self.player.character.main_inst.HumanoidRootPart
	self.Humanoid = self.player.character.main_inst.Humanoid
	local wayFolder = workspace:FindFirstChild("WayTracePoints"):FindFirstChild("Model") -- 替换为你的文件夹路径
    if not wayFolder then
        warn("Folder 'Way' not found in workspace!")
        return
    end
    
    local parts = {} -- 用来存储按编号排序的 Part
    
    -- 遍历文件夹中的所有子对象
    for _, child in pairs(wayFolder:GetChildren()) do
        if child:IsA("BasePart") and tonumber(child.Name) then
            table.insert(parts, child)
        end
    end
    
    -- 按名称（编号）排序
    table.sort(parts, function(a, b)
        return tonumber(a.Name) < tonumber(b.Name)
    end)
    for _, part in ipairs(parts) do
        print("Part Name:", part.Name, "Position:", part.Position)
    end
    
	local speed  = 50
    for _, part in ipairs(parts) do
		print("朝向的Part",part)
		local distance = 0
        local time = math.Huge
		if part.Name == "1" then
			print("111")
			distance = (st1.Position - part.Position).Magnitude
			
		else
			print("222")
			distance = (self.RootPart.Position - part.Position).Magnitude
           
		end
        time = 0.9*distance/speed
		print("等待",distance,time)
        self.Humanoid:MoveTo(part.Position)
        task.wait(time)
    end


-- Confirm the computation was successful


end
function M.CreatePVPTrack(self,isWin,rivalId,myCarData,rivalCarData)
	self.hadOver = false

	local TrackAttRoot=game.Workspace:WaitForChild("PVPTrack")
	local st1 = TrackAttRoot:WaitForChild("Start01")
	local st2 = TrackAttRoot:WaitForChild("Start02")

    self.HeartbeatUpdateConnect = nil
    self:ClearPVPTrack()
    if not isWin then
        print("本次PVP:我方将爆炸————————")
    else
        print("本次PVP:对方将爆炸————————")
    end
	if myCarData==nil or rivalCarData==nil then
		print("car 走的假测试数据")
		---参数：1player，2是否是玩家乘坐的，3-7部件编号，8是否发生爆炸
		self.Car=_G.CommonOHOH.load.PVPCar().new(self.player,true,"10003_1","50001_1","20001_1","30001_1","40001_1",not isWin)
		self.EnemyCar=_G.CommonOHOH.load.PVPCar().new(self.player,false,"10003_1","50001_1","20001_1","30001_1","40001_1",isWin)
	else
		print("car 走的数据库数据")
		self.Car=_G.CommonOHOH.load.PVPCar().new(self.player,true,myCarData[1],myCarData[2],myCarData[3],myCarData[4],myCarData[5],not isWin)
		self.EnemyCar=_G.CommonOHOH.load.PVPCar().new(self.player,false,rivalCarData[1],rivalCarData[2],rivalCarData[3],rivalCarData[4],rivalCarData[5],isWin,rivalId)
	end

    self.EnemyCar:SetCFrame(st2.CFrame)
    self.EnemyCar:SetParent(workspace)

    self.Car:SetCFrame(st1.CFrame)
    self.Car:SetParent(workspace)

    if self.HeartbeatUpdateConnect then
		self.HeartbeatUpdateConnect=nil
		self.HeartbeatUpdateConnect:Disconnect()
	end
	self.HeartbeatUpdateConnect=RunService.Heartbeat:Connect(function(dt)
        if self.Car and  self.EnemyCar then
			local test = self.Car:GetWeapon():GetTouchPart()
			local test1 = self.EnemyCar:GetWeapon():GetTouchPart()
			local del =  (test.Position-test1.Position).Magnitude
			if self.Disdance then
				if self.Disdance< del and del<200 then
					if  not self.hadBoom then
						self.hadBoom=true
						local boomCar = nil
						if self.Car.isBoom then
							boomCar = self.Car
						else 
							boomCar = self.EnemyCar
						end
						if boomCar then
							boomCar:CarCrash()
							-- self.hadBoom=true
							boomCar.isBoom = false
							-- task.wait(4);
							boomCar.PVPEndEvent:Action()
						end

					end
				end
			end
			self.Disdance = del
			
		end

    end)

    self.Car:Start() --开始一局
    self.EnemyCar:Start()
	
	self.PVPOverEvent:Connect(function(isAlive)
		if self.hadOver then
			return
		end
		self.hadOver =true

       
        if self.player.main_inst then
			if isAlive then
				print("对方爆炸了——————")
				self.parent:Invoke(FunctionList.pvp_rebirth)
			else
				print("我爆炸了——————")
				if self.player.main_inst.Character.Humanoid.Health > 0 then
					self.player.main_inst.Character.Humanoid.Health  = 0
				end
			end	
            -- self.player.main_inst:LoadCharacter()
            if self.player.main_inst:FindFirstChild("ForceField") then
                self.player.main_inst.ForceField:Destroy()
            end
        end
        if self.HeartbeatUpdateConnect then
			self.HeartbeatUpdateConnect:Disconnect()
            self.HeartbeatUpdateConnect=nil
        end
		self:ClearPVPTrack()
		self.hadBoom = false
		PVPGui:PVPEnd()
	end)
	
    self.EnemyCar.PVPEndEvent:Connect(function()
		self.PVPOverEvent:Action(true)
    end)

    self.Car.PVPEndEvent:Connect(function()
		self.PVPOverEvent:Action(fasle)
    end)


end

return M
